package com.lagou.servlet;

import com.lagou.factory.ProxyFactory;
import com.lagou.service.AccountService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: Tengfei.Wang
 * @Description;
 * @Date: 1/9/2020
 * @Modified by:
 */
public class AccountServlet extends HttpServlet {

    private AccountService accountService;

    @Override
    public void init() throws ServletException {
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ProxyFactory proxyFactory = (ProxyFactory) applicationContext.getBean("proxyFactory");
        accountService = (AccountService) proxyFactory.getProxy(applicationContext.getBean("accountService"));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        accountService.transferMoney("622001", "622002", 100);
    }
}
